//
//  PublicCommentViewController.m
//  CarFace
//
//  Created by dlios on 15-4-3.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "PublicCommentViewController.h"
#import "PublicComment.h"
#import "Ser_CommentTableViewCell.h"

@interface PublicCommentViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)UIImageView *participateImg;
@property (nonatomic, retain)UILabel *average;
@property (nonatomic, retain)UILabel *peoplenum;
@property (nonatomic, retain)UIImageView *oilCostImg;
@property (nonatomic, retain)UILabel *fuelconsumption;
@property (nonatomic, retain)UILabel *oilpeoplenum;
@property (nonatomic, retain)NSString *averagehh;


@end

@implementation PublicCommentViewController

- (void)dealloc
{
    [_tableView release];
    [_array release];
    [_participateImg release];
    [_average release];
    [_peoplenum release];
    [_oilCostImg release];
    [_fuelconsumption release];
    [_oilpeoplenum release];
    [_averagehh release];
    [super dealloc];
    
}

-(void)handleData
{
    //初始化数组
    self.array = [NSMutableArray array];
    
    NSString *str = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.6/alibi/specalibilist-a2-pm2-v4.6.1-sp%@-p1-s20.html",self.modelTypeID];
    NSLog(@"%@",str);
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError *error = nil;
        NSJSONSerialization *result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        NSDictionary *dic = [result valueForKey:@"result"];
        self.average.text = [NSString stringWithFormat:@"%@分",[dic valueForKey:@"average"]];
        self.peoplenum.text = [NSString stringWithFormat:@"%@人参与",[dic valueForKey:@"peoplenum" ]];

        self.averagehh =[dic valueForKey:@"average"];
        self.fuelconsumption.text = [NSString stringWithFormat:@"%@升/百公里",[dic valueForKey:@"fuelconsumption"]];
        self.oilpeoplenum.text = [NSString stringWithFormat:@"%@人提供",[dic valueForKey:@"oilpeoplenum"]];
        
        NSArray *arr = [[result valueForKey:@"result"]valueForKey:@"grades"];
        for (NSDictionary *dic2 in arr) {
            PublicComment *info = [[PublicComment alloc]initWithDictionary:dic2];
            [self.array addObject:info];
            [info release];
        }
        NSLog(@"?????%@",arr);
//        [self.tableView reloadData];
        
        if ([self.averagehh isEqualToString:@"0.00"]) {
//            UIView *view = [[UIView alloc]initWithFrame:self.view.bounds];
            UILabel *lable = [[UILabel alloc]initWithFrame:self.view.bounds];
            lable.backgroundColor = [UIColor whiteColor];
            lable.text= @"此车系暂无评分!!!";
            lable.textAlignment = NSTextAlignmentCenter;
            [self.view addSubview:lable];
            [lable release];
            
        }
    [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败%@",error);
    }];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self handleData];
    self.title = @"评分";
    self.view.backgroundColor = [UIColor whiteColor];
    self.scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
//    self.scrollView.backgroundColor = [UIColor yellowColor];
//    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    
    
    //在scrollview上添加左右两个view,并在每个view上添加一个图片页面和两个label
    //添加左边view
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width/2, self.scrollView.frame.size.height/6)];
//    leftView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:leftView];
    [leftView release];
    //左边view的图片
    self.participateImg = [[UIImageView alloc]initWithFrame:CGRectMake(leftView.frame.size.width/4-10, leftView.frame.size.height/4, leftView.frame.size.width/4*2/3+10, leftView.frame.size.height/4*2)];
//    self.participateImg.backgroundColor = [UIColor whiteColor];
    self.participateImg.image = [UIImage imageNamed:@"takepate.png"];
    [leftView addSubview:self.participateImg];
    [_participateImg release];
    
    //左边view的评分label
    self.average = [[UILabel alloc]initWithFrame:CGRectMake(leftView.frame.size.width/4+leftView.frame.size.width/4*2/3, leftView.frame.size.height/4, leftView.frame.size.width/4*3, leftView.frame.size.height/4*2/3*2)];
//    self.average.backgroundColor = [UIColor yellowColor];
    [leftView addSubview:self.average];
    [_average release];
    //左边view的人数label
    self.peoplenum = [[UILabel alloc]initWithFrame:CGRectMake(leftView.frame.size.width/4+leftView.frame.size.width/4*2/3, leftView.frame.size.height/4+leftView.frame.size.height/4*2/3*2, leftView.frame.size.width/4*3, leftView.frame.size.height/4*2/3)];
//    self.peoplenum.backgroundColor = [UIColor whiteColor];
    [leftView addSubview:self.peoplenum];
    [_peoplenum release];
    
    
    //添加右边的view
    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width/2, 0, self.scrollView.frame.size.width/2, self.scrollView.frame.size.height/6)];
//    rightView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:rightView];
    [rightView release];
    
    //右边view的图片
    self.oilCostImg = [[UIImageView alloc]initWithFrame:CGRectMake(rightView.frame.size.width/4-10, rightView.frame.size.height/4, rightView.frame.size.width/4*2/3+10, rightView.frame.size.height/4*2)];
//    self.oilCostImg.backgroundColor = [UIColor whiteColor];
    self.oilCostImg.image = [UIImage imageNamed:@"oilneed.png"];
    [rightView addSubview:self.oilCostImg];
    [_oilCostImg release];
    
    
    self.fuelconsumption = [[UILabel alloc]initWithFrame:CGRectMake(rightView.frame.size.width/4+rightView.frame.size.width/4*2/3, rightView.frame.size.height/4, rightView.frame.size.width/4*3, rightView.frame.size.height/4*2/3*2)];
//    self.fuelconsumption.backgroundColor = [UIColor yellowColor];
    [rightView addSubview:self.fuelconsumption];
    [_fuelconsumption release];
    
    self.oilpeoplenum =[[UILabel alloc]initWithFrame:CGRectMake(rightView.frame.size.width/4+rightView.frame.size.width/4*2/3, rightView.frame.size.height/4+rightView.frame.size.height/4*2/3*2, rightView.frame.size.width/4*3, rightView.frame.size.height/4*2/3)];
//    self.oilpeoplenum.backgroundColor = [UIColor whiteColor];
    [rightView addSubview:self.oilpeoplenum];
    [_oilpeoplenum release];
    CGFloat W = self.scrollView.frame.size.width;
    CGFloat H = self.scrollView.frame.size.height;

    UILabel *itemName = [[UILabel alloc]initWithFrame:CGRectMake(10, H/6+10,W/9+5+(W-10-W/9-35)/3, H/18)];
    itemName.text = @"项目名称";
//    itemName.backgroundColor = [UIColor colorFromHexCode:@"#224b8f"];
    itemName.textColor = [UIColor colorFromHexCode:@"#003a6c"];
//    itemName.layer.cornerRadius = 20;
    
    [self.scrollView addSubview:itemName];
    [itemName release];
    UILabel *grade = [[UILabel alloc]initWithFrame:CGRectMake(10+10+W/9+5+(W-10-W/9-35)/3, H/6+10, (W-10-W/9-35)/3, H/18)];
    grade.text = @"本款评分";
//    grade.backgroundColor = [UIColor colorFromHexCode:@"#224b8f"];
    grade.textColor = [UIColor colorFromHexCode:@"#003a6c"];
//    grade.layer.cornerRadius = 10;
    [self.scrollView addSubview:grade];
    [grade release];
    
    UILabel *levelGrade = [[UILabel alloc]initWithFrame:CGRectMake(10+10+10+W/9+5+(W-10-W/9-35)/3*2, H/6+10, (W-10-W/9-35)/3, H/18)];
    levelGrade.text = @"行业平均";
//    levelGrade.backgroundColor = [UIColor colorFromHexCode:@"#224b8f"];
    levelGrade.textColor = [UIColor colorFromHexCode:@"#003a6c"];
//    levelGrade.layer.cornerRadius = 10;
    [self.scrollView addSubview:levelGrade];
    [levelGrade release];
    
    //设置tableView
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, H/6+10+H/18, W, H-(H/6+10+H/18)-113)];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
//    self.tableView.scrollEnabled = NO;
    [_tableView release];
    

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"jjj";
    Ser_CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[Ser_CommentTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]autorelease];
        
    }
    
    PublicComment *info = [self.array objectAtIndex:indexPath.row];
    cell.info = info;
    NSInteger i = 0;
    i = indexPath.row;
    
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"s%ld",indexPath.row]];
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 50;
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
